﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using WpfPractice.MVVM;

namespace WpfPractice.ViewModel
{
    public class CircularMenuViewModel : ObservableObject
    {
        public CircularMenuViewModel()
        {
            this.Init();
        }
        //菜单数量
        private readonly int _MenuCount = 8;
        //菜单名称，实际上就是我们执行的原生命令的ID标识
        public List<string> CommandId { get; set; } = new List<string>()
        {
            "ID_OBJECTS_WALL",
            "ID_OBJECTS_DOOR",
            "ID_OBJECTS_WINDOW",
            "ID_OBJECTS_ROOM",
            "ID_OBJECTS_BEAM",
            "ID_OBJECTS_FLOOR",
            "ID_SEL_ALL_IN_VIEW",
            "ID_TEMPHIDE_ISOLATECAT"
        };
        //绑定给自定义控件
        private ObservableCollection<MenuItemModel> _elements = new ObservableCollection<MenuItemModel>();
        public ObservableCollection<MenuItemModel> Elements
        {
            get => _elements;
            set => SetProperty(ref _elements, value);
        }
        private void Init()
        {
            var angle = 0;
            for (int i = 1; i <= _MenuCount; i++)
            {
                var brushConverter = new BrushConverter();
                var brush = (Brush)brushConverter.ConvertFromString("#BAE766");
                if ((i % 2) == 1)
                    brush = (Brush)brushConverter.ConvertFromString("#B0D440");
                this.Elements.Add(new MenuItemModel
                {
                    Angle = angle,
                    Title = this.CommandId[i - 1],
                    FillColor = brush,
                    IconImage = new BitmapImage(new Uri($"pack://application:,,,/Assets/favicon.ico")),
                    MyCommand = new DelegateCommand(ExecuteMyCommand),
                });
                angle += 45;
            }
        }
        private void ExecuteMyCommand(object parameter)
        {
            // 您点击时要执行的逻辑, parameter参数将是MenuTxt绑定的值
            var menuText = parameter as string;
            MessageBox.Show(menuText);
            //【1】viewModel发送消息到View后台，关闭窗口
            //【2】viewModel定义了view变量,在view后台代码赋值
            //【3】viewModel中的command中执行事件，该事件在View后台使用关闭窗口的方法赋值
            CloseRequested?.Invoke();
        }
        //方便我们在view中为其使用关闭窗口的方法赋值
        public event Action CloseRequested;

    }
    public class MenuItemModel
    {
        public double Angle { get; set; }
        public string Title { get; set; }
        public Brush FillColor { get; set; }
        public ImageSource IconImage { get; set; }
        public DelegateCommand MyCommand { get; set; }
    }

}
